package Hystrix;

import com.netflix.hystrix.*;

import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * author:Yhk
 * Date:2019/4/11
 * Time:9:57 AM
 */
public class HelloWorldCommand extends HystrixCommand<String> {
    private final String name;

    public HelloWorldCommand(String name) {
        //最少配置:指定命令组名(CommandGroup)
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("query"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ExampleThreadPool"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(20))//服务线程池数量
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionTimeoutInMilliseconds(500)
                        .withCircuitBreakerErrorThresholdPercentage(60) //熔断器关闭到打开阈值
                        .withCircuitBreakerSleepWindowInMilliseconds(3000)//熔断器打开到关闭的时间窗长度
                ));
        //super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }

    @Override
    protected String getFallback() {
        System.out.println("chaoshi");
        return super.getFallback();
    }

    @Override
    protected String run() throws Exception {
        TimeUnit.MILLISECONDS.sleep(1000);
        return "Hello " + name + " thread:" + Thread.currentThread().getName();
    }

    //调用实例
    public static void main(String[] args) throws Exception {
        //每个Command对象只能调用一次,不可以重复调用,
        //重复调用对应异常信息:This instance can only be executed once. Please instantiate a new instance.
        HelloWorldCommand helloWorldCommand = new HelloWorldCommand("Synchronous-hystrix");
        //使用execute()同步调用代码,效果等同于:helloWorldCommand.queue().get();
        String result = helloWorldCommand.execute();
        System.out.println("result=" + result);

        helloWorldCommand = new HelloWorldCommand("Asynchronous-hystrix");
        //异步调用,可自由控制获取结果时机,
//        Future<String> future = helloWorldCommand.queue();
//        //get操作不能超过command定义的超时时间,默认:1秒
//        result = future.get(1000000000, TimeUnit.MILLISECONDS);
//        System.out.println("result=" + result);
//        System.out.println("mainThread=" + Thread.currentThread().getName());


//        Observable<String> observable = new HelloWorldCommand("doge observable").observe();
//
//        observable.subscribe(result1 -> System.out.println("Observable call result --> " + result1));
//        observable.subscribe(new Observer<String>() {
//            @Override
//            public void onCompleted() {
//                //onNext/onError完成之后最后回调
//                System.out.println("Execute onCompleted");
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//                // 当产生异常时回调
//                System.out.println("Execute error");
//                throwable.printStackTrace();
//            }
//
//            @Override
//            public void onNext(String s) {
//                // 获取结果后回调
//                System.out.println("Execute onNext --> " + s);
//            }
//        });
    }

}
